# 整体案例
import pandas as pd

df = pd.read_csv('datasets_ML2/phone.txt', header=None, names=['x', 'y'])
print(df.head())

print("df.corr():", df.corr())

# 数据集分析
# 去重数据集中的重复值   重复值会导致模型发生过拟合问题
df.drop_duplicates(inplace=True)
print(df.info())

# 拆分x，y
x = df['x'].values.reshape(-1, 1)
y = df['y'].values.reshape(-1, 1)

# 特征缩放
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
x = std.fit_transform(x)

# 切分数据集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

# 创建模型
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x_train, y_train)
h = model.predict(x_test)

# 底层计算R2
import numpy as np
mse = np.mean(np.square(y_test - h))
sigma2 = np.mean(np.square(y_test - y_test.mean()))
R2 = 1 - mse / sigma2
print("R2: ", R2)

print("theta: ", model.intercept_, model.coef_)
theta0 = model.intercept_
theta1 = model.coef_[0]

x_val = np.array([x.min(), x.max()])
y_val = theta0 + theta1 * x_val

# data visualization
import matplotlib.pyplot as plt
plt.scatter(x, y)
plt.plot(x_val, y_val, 'r-')
plt.show()

# 调库进行模型评测
from sklearn.metrics import mean_squared_error, r2_score
import warnings
warnings.filterwarnings('ignore')

print('均方误差', mean_squared_error(y_test, h))
print('线性回归得分', r2_score(y_test, h))

# 上面线性回归模型效果不好，可以使用L2正则化去进行拟合数据
from sklearn.linear_model import Ridge
ridge = Ridge()
from sklearn.model_selection import GridSearchCV

# 网格搜索交叉验证   查找最优参数
# 使用L2正则模型进行交叉验证，分别使用正则化系数为0.1, 0.3, 0.5, 0.7, 1.0进行评测， 5折交叉验证

model = GridSearchCV(ridge, param_grid={'alpha': [0.1, 0.3, 0.5, 0.7, 1.0]}, cv=5)
model.fit(x_train, y_train)
print('模型的最优参数:{}'.format(model.best_params_))
print('模型的得分：{}'.format(model.best_score_))

# 重新创建模型
model = Ridge(alpha=0.5)
model.fit(x_train, y_train)
print('调整后得分：', model.score(x_test, y_test))
